|
|
PREAMBLE Too busy to learn C++? Basically, using C++ compiler as a better C is not a good idea: you should master full feature of C++ language especially when you are developing a large software. However, you are too busy to learn another language. Okay, I'll show you a subset of ISO C++ feature that has still sufficient power to boost up your programming cycle. Which C++ compiler? Just use ISO C++. The ISO C++ is much improved from classical C++ flavors (including ARM 2nd, the ANSI C++ base document.) For example, Gnu's g++ 2.95 is a good C++ compiler: it supports almost full feature of ISO C++ language and the standard library. USING ISO C++ Use C++ style comment The most popular feature added to C is the new comment style.
Namespace The namespace is a newly added
keyword of ISO C++. It
provides user controllable name space that means the prefix of function
name is no longer needed when you write some library.
Don't use constant macro, use constant value The const value in C++ is truely constant. It is always a better replacement for #define constant definition. (Putting const value definitions in header files is never stupid idea in C++.) For example, write
instead of using macro as follows:
The const value has its type so that the compiler can check ill usage of constant value; while the macro does not have type, because the macro is preprocessed before compiling.
Don't use function macro, use inline function, or use function template The following function (called inline function)
is a modern replacement of the following macro.
Some of you may claim that the macro version is still better because it is not type-checked. For example,
is commonly used macro as a generic (untyped)
function. To obtain such generic function without macro, C++
provides template. See the following example.
You can put automatic value definitions almost everywhere In C++, you can put definitions of automatic value almost everywher. For example,
is commonly used technique to localize loop-counter into the loop itself. Use string, don't use char-array The ISO C++ standard library provides string type.
The standard library of ISO C++ covers all features appeared in ANSI C standard library. See ISO C++ reference book (e.g. Bjarne Stroustrup: The C++ Programming Language Third Edition) for more detail. Use iostream instead of stdio Forget all about <stdio.h>. The use of printf is considerably dangerous because the printf nomaly lacks argument type checking.
The stdout corresponds to cout and, as you guess, the stdin corresponds to cin, the stderr corresponds to cerr. Here's an example of cin.
Do not use array, try "vector" There is no array type in C. There are only pointer and continuos memory allocation method. In ISO C++, the standard library provides a true array, named vector.
Though the vector itself only provides 1D array, you can define the vector of vector by recursive use of vector as follows.
You can also define something like matrix type by yourself, but definition of user defined type (or class) requires some knowledge of C++. Try "map" See this example. I'm not joking. The example really works.
Forget "malloc", use "new" operaotr The C++ prohibits conversion from void* to any other pointer type. That means, the malloc is completely obsolete. The C++ gives you a new operator to allocate memory.
Don't use free for freeing memory allocated by new operator. Use delete instead.
If you really want to allocate array,
And free it in this way.
Anyway, you should consider vector before trying array style memory allocation.
Use "static cast" If you can, avoid casting. The implicit casting rule is clearly documented in C++, it is much better than Pascal. If you really want to cast something to other type, use ISO C++ style casting to give a chance to the compiler to check the casting safe or not.
Use exception, instead of returning zero, or providing call-back mechanism You can throw an exception if something goes wrong in your
function. And later, someone (including you) can catch the
exception.
Use "reference" and "const reference" With the C++, you can define arguments of functions as call-by-reference. For example,
is really similar to
The difference appears only at calling this function.
Reference operaotr & is no longer necessary unless you use C style function. The first code is as exactly same as following Pascal procedure.
The const reference is worth using to pass non-small object to a function.
Boolean You can use real boolean (bool) in ISO C++, that holds true or false. The bool type can be implicitly converted to int with value of 1 (true) or 0 (false). @#$%&!?, use "and", "or", "not" keywords This is a good news if, only if, you love Pascal. Next table shows newly added keywords to C++.
CONCLUSION I recommend true ISO C++ as your primary language for Unix programming. Due to its object oriented programming feature, ISO C++ is a very natural way to construct a relatively large software. Last update: March 22, 2000 |